package com.lagou.mapreduce;

import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class HomeworkBean implements WritableComparable<HomeworkBean> {
//封装HomeworkBean对象当做key传输
    private int id;
    private int num;

    public HomeworkBean() {
    }

    public HomeworkBean(int id, int num) {
        this.id = id;
        this.num = num;
    }

    //重写compareTo方法进行排序
    @Override
    public int compareTo(HomeworkBean o) {
        if(this.num > o.num){
            return 1;
        }else if(this.num < o.num){
            return -1;
        }else {
            return 0;
        }
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(id);
        out.writeInt(num);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.id = in.readInt();
        this.num = in.readInt();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    @Override
    public String toString() {
        return id + "\t" + num;
    }
}
